/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package gui;

import model.MaterialDirection;
import draw.ColorSet;
import exceptions.NoNameGivenException;
import exceptions.ValueNotBetweenZeroAndOneException;
import model.Epoxy;
import model.Fiber;
import model.Lamel;

import java.awt.Color;
import java.awt.event.FocusEvent;
import javax.swing.JColorChooser;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

/**
 *
 * @author Memet
 */
public class EditMaterial extends javax.swing.JFrame {
    private Lamel m;
    private Fiber f;
    private Epoxy e;
    private double vf;
    private Color fiberColor;
    private Color epoxyColor;
    private MaterialDirection soort;
    private boolean manualChange;
    /**
     * Creates new form EditMaterial
     */
    public EditMaterial() {
        initComponents();
        f = null;
        e = null;
        vf = 0.5;
        soort = MaterialDirection.UNIDIRECTIONAAL;
        m = null;
        fiberColor = Color.red;
        epoxyColor = Color.white;
        manualChange = true;
    }

    public void setMaterial(Lamel m)
    {
        this.m = m;
        fiberColor = m.getColorSet().getFiber();
        epoxyColor = m.getColorSet().getBackground();
        //soort = m.getSoort();
        txtDikte.setText(String.valueOf(m.getThickness()));
        vf = m.getVf();
        txtFabrikant.setText(m.getManufacturer());
        txtNaam.setText(m.getName());
        updateForm();
        cbEpoxy.setSelectedItem(m.getMyEpoxy());
        cbFibers.setSelectedItem(m.getMyFiber());
        f = m.getMyFiber();
        e = m.getMyEpoxy();

        cbType.setSelectedIndex(2);
        if(m.getType().equals(MaterialDirection.UNIDIRECTIONAAL))
        {
            cbType.setSelectedIndex(0);
        }
        else if(m.getType().equals(MaterialDirection.MINVIJFENVEERTIGVIJFENVEERTIG))
        {
            cbType.setSelectedIndex(1);
        }

    }
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        slVf = new javax.swing.JSlider();
        txtVf = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        cbType = new javax.swing.JComboBox();
        jPanel3 = new javax.swing.JPanel();
        examp = new draw.pnlExample();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        btnFiberColor = new javax.swing.JButton();
        btnEpoxyColor = new javax.swing.JButton();
        jLabel3 = new javax.swing.JLabel();
        txtFabrikant = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        txtNaam = new javax.swing.JTextField();
        jLabel6 = new javax.swing.JLabel();
        txtDikte = new javax.swing.JTextField();
        btnEditMaterial = new javax.swing.JButton();
        cbFibers = new components.SteppedComboBox();
        cbEpoxy = new components.SteppedComboBox();
        jLabel18 = new javax.swing.JLabel();
        jLabel19 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jPanel1.setBackground(new java.awt.Color(255, 255, 255));

        slVf.setBackground(new java.awt.Color(255, 255, 255));
        slVf.setForeground(new java.awt.Color(102, 51, 102));
        slVf.setMajorTickSpacing(100);
        slVf.setMaximum(1000);
        slVf.setMinorTickSpacing(1);
        slVf.setPaintTicks(true);
        slVf.setSnapToTicks(true);
        slVf.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                slVfStateChanged(evt);
            }
        });

        txtVf.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        txtVf.setText("0");
        txtVf.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                txtVfFocusLost(evt);
            }
        });
        txtVf.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                txtVfKeyPressed(evt);
            }
        });

        jLabel1.setText("Fiber volume fraction");

        cbType.setFont(new java.awt.Font("Rockwell", 1, 11)); // NOI18N
        cbType.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "UD", "-45° / + 45°", "0° / 90°" }));
        cbType.addPopupMenuListener(new javax.swing.event.PopupMenuListener() {
            public void popupMenuCanceled(javax.swing.event.PopupMenuEvent evt) {
            }
            public void popupMenuWillBecomeInvisible(javax.swing.event.PopupMenuEvent evt) {
                cbTypePopupMenuWillBecomeInvisible(evt);
            }
            public void popupMenuWillBecomeVisible(javax.swing.event.PopupMenuEvent evt) {
            }
        });


        jPanel3.setBackground(new java.awt.Color(255, 255, 255));

        examp.setBorder(new javax.swing.border.LineBorder(new java.awt.Color(255, 153, 0), 1, true));

        javax.swing.GroupLayout exampLayout = new javax.swing.GroupLayout(examp);
        examp.setLayout(exampLayout);
        exampLayout.setHorizontalGroup(
            exampLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 48, Short.MAX_VALUE)
        );
        exampLayout.setVerticalGroup(
            exampLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 18, Short.MAX_VALUE)
        );

        jLabel4.setText("Fiber plot color");

        jLabel5.setText("Epoxy plot color");

        btnFiberColor.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnFiberColorActionPerformed(evt);
            }
        });

        btnEpoxyColor.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnEpoxyColorActionPerformed(evt);
            }
        });

        jLabel3.setFont(new java.awt.Font("Rockwell", 1, 11)); // NOI18N
        jLabel3.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel3.setText("Manufacturer");

        txtFabrikant.setHorizontalAlignment(javax.swing.JTextField.CENTER);

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addGap(96, 96, 96)
                .addComponent(examp, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(114, 114, 114))
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jLabel5))
                        .addGap(72, 72, 72)
                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(btnFiberColor, javax.swing.GroupLayout.PREFERRED_SIZE, 73, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(btnEpoxyColor, javax.swing.GroupLayout.PREFERRED_SIZE, 73, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addComponent(txtFabrikant)
                    .addComponent(jLabel3, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addComponent(examp, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel4)
                    .addComponent(btnFiberColor, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel5)
                    .addComponent(btnEpoxyColor, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel3)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(txtFabrikant, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(29, 29, 29))
        );

        jLabel2.setFont(new java.awt.Font("Rockwell", 1, 11)); // NOI18N
        jLabel2.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel2.setText("Name");

        txtNaam.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        txtNaam.setText("Material01");

        jLabel6.setFont(new java.awt.Font("Rockwell", 1, 11)); // NOI18N
        jLabel6.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel6.setText("Thickness [mm]");

        txtDikte.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        txtDikte.setText("1");

        btnEditMaterial.setFont(new java.awt.Font("Rockwell", 1, 11)); // NOI18N
        btnEditMaterial.setText("Edit Material");
        btnEditMaterial.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnEditMaterialActionPerformed(evt);
            }
        });

        cbFibers.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
        cbFibers.addPopupMenuListener(new javax.swing.event.PopupMenuListener() {
            public void popupMenuCanceled(javax.swing.event.PopupMenuEvent evt) {
            }
            public void popupMenuWillBecomeInvisible(javax.swing.event.PopupMenuEvent evt) {
                cbFibersPopupMenuWillBecomeInvisible(evt);
            }
            public void popupMenuWillBecomeVisible(javax.swing.event.PopupMenuEvent evt) {
            }
        });


        cbEpoxy.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
        cbEpoxy.addPopupMenuListener(new javax.swing.event.PopupMenuListener() {
            public void popupMenuCanceled(javax.swing.event.PopupMenuEvent evt) {
            }
            public void popupMenuWillBecomeInvisible(javax.swing.event.PopupMenuEvent evt) {
                cbEpoxyPopupMenuWillBecomeInvisible(evt);
            }
            public void popupMenuWillBecomeVisible(javax.swing.event.PopupMenuEvent evt) {
            }
        });

        jLabel18.setFont(new java.awt.Font("Rockwell", 1, 11)); // NOI18N
        jLabel18.setText("Fiber");

        jLabel19.setFont(new java.awt.Font("Rockwell", 1, 11)); // NOI18N
        jLabel19.setText("Epoxy");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(btnEditMaterial, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(txtNaam, javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(cbType, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jLabel6, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(txtDikte)
                            .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                                .addComponent(slVf, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(txtVf, javax.swing.GroupLayout.PREFERRED_SIZE, 69, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addComponent(jPanel3, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addComponent(jLabel18, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jLabel19, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(cbEpoxy, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(cbFibers, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(cbFibers, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel18))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(cbEpoxy, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel19))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(14, 14, 14)
                        .addComponent(jLabel1))
                    .addComponent(slVf, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txtVf, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(cbType, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel6)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(txtDikte, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(txtNaam, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, 123, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addComponent(btnEditMaterial)
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void slVfStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_slVfStateChanged
        if (manualChange) {
            vf = slVf.getValue() / 100;
            vf /= 10;
            updateForm();
        } else {
            manualChange = true;
        }
    }//GEN-LAST:event_slVfStateChanged

    private void txtVfFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_txtVfFocusLost
        String v = txtVf.getText();
        double val = Double.parseDouble(v);
        if (val < 0.0) {
            vf = 0;
        } else if (val > 1.0) {
            vf = 1;
        } else {
            vf = val;
        }
        updateForm();
    }//GEN-LAST:event_txtVfFocusLost

    private void txtVfKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtVfKeyPressed
        if (evt.getKeyCode() == evt.VK_ENTER) {
            txtVfFocusLost(new FocusEvent(slVf, WIDTH));
        }
    }//GEN-LAST:event_txtVfKeyPressed

    private void cbTypePopupMenuWillBecomeInvisible(javax.swing.event.PopupMenuEvent evt) {//GEN-FIRST:event_cbTypePopupMenuWillBecomeInvisible
        soort = MaterialDirection.NULNEGENTIG;

        if (cbType.getSelectedIndex() == 0) {
            soort = MaterialDirection.UNIDIRECTIONAAL;

        } else if (cbType.getSelectedIndex() == 1) {
            soort = MaterialDirection.MINVIJFENVEERTIGVIJFENVEERTIG;

        }

        updateForm();
    }//GEN-LAST:event_cbTypePopupMenuWillBecomeInvisible



    private void btnFiberColorActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnFiberColorActionPerformed
        JColorChooser cc = new JColorChooser();
        cc.setPreviewPanel(new JPanel());
        cc.repaint();
        Color color = cc.showDialog(rootPane, "Choose a Color", Color.red);
        if (color != null) {
            fiberColor = color;
        }
        btnFiberColor.setBackground(fiberColor);
        updateForm();
    }//GEN-LAST:event_btnFiberColorActionPerformed

    private void btnEpoxyColorActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnEpoxyColorActionPerformed
        JColorChooser cc = new JColorChooser();
        cc.setPreviewPanel(new JPanel());
        cc.repaint();
        Color color = cc.showDialog(rootPane, "Choose a Color", Color.red);
        if (color != null) {
            epoxyColor = color;
        }
        btnEpoxyColor.setBackground(epoxyColor);
        updateForm();
    }//GEN-LAST:event_btnEpoxyColorActionPerformed

    private void btnEditMaterialActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnEditMaterialActionPerformed
        updateForm();
        try {
            soort = MaterialDirection.NULNEGENTIG;
            if (cbType.getSelectedIndex() == 0) {
                soort = MaterialDirection.UNIDIRECTIONAAL;
            } else if (cbType.getSelectedIndex() == 1) {
                soort = MaterialDirection.MINVIJFENVEERTIGVIJFENVEERTIG;
            }
            if (txtNaam.getText().equalsIgnoreCase("")) {
                throw new NoNameGivenException();
            }
            Lamel lamel = new Lamel(f, e, vf);
            lamel.setThickness(Double.parseDouble(txtDikte.getText()));
            lamel.setName(txtNaam.getText());
            lamel.setColorSet(new ColorSet(epoxyColor, fiberColor));
            lamel.setManufacturer(txtFabrikant.getText());
            /* todo refactor
            Global.data.setLamel(m, lamel);*/
        } catch (ValueNotBetweenZeroAndOneException ex) {
            JOptionPane.showMessageDialog(rootPane, "The Fibervolumefraction has to be between zero and one");
        } /*catch (BelowOrEqualsToZeroException ex) {
            JOptionPane.showMessageDialog(rootPane, "The thickness of the material has to be higher than 0");
        } */catch (NoNameGivenException ex) {
            JOptionPane.showMessageDialog(rootPane, "Please enter a name for the material");
        }
        /* todo refactor
        if (txtNaam.getText().equals("Material" + Global.data.geefNieuweMaterialNaam())) {
            Global.data.gebruikNieuweMaterialNaam();
        }
        Global.menu.editMaterialButton(true);
        Global.menu.refreshScreens();
        this.dispose();              */
    }//GEN-LAST:event_btnEditMaterialActionPerformed

    private void cbFibersPopupMenuWillBecomeInvisible(javax.swing.event.PopupMenuEvent evt) {//GEN-FIRST:event_cbFibersPopupMenuWillBecomeInvisible
        if (cbFibers.getSelectedItem() != null) {
            getFiberSelection();
        } else {
            fiberColor = Color.red;
            updateForm();
        }
    }//GEN-LAST:event_cbFibersPopupMenuWillBecomeInvisible



    private void cbEpoxyPopupMenuWillBecomeInvisible(javax.swing.event.PopupMenuEvent evt) {//GEN-FIRST:event_cbEpoxyPopupMenuWillBecomeInvisible
        if (cbEpoxy.getSelectedItem() != null) {
            getEpoxySelection();
        } else {
            epoxyColor = Color.white;
            updateForm();
        }
    }//GEN-LAST:event_cbEpoxyPopupMenuWillBecomeInvisible

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnEditMaterial;
    private javax.swing.JButton btnEpoxyColor;
    private javax.swing.JButton btnFiberColor;
    private components.SteppedComboBox cbEpoxy;
    private components.SteppedComboBox cbFibers;
    private javax.swing.JComboBox cbType;
    private draw.pnlExample examp;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel18;
    private javax.swing.JLabel jLabel19;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JSlider slVf;
    private javax.swing.JTextField txtDikte;
    private javax.swing.JTextField txtFabrikant;
    private javax.swing.JTextField txtNaam;
    private javax.swing.JTextField txtVf;
    // End of variables declaration//GEN-END:variables

    private void getFiberSelection() {
        f = (Fiber)cbFibers.getSelectedItem();
        fiberColor = f.getColor();
        updateForm();
    }

    private void updateForm() {
        int selectionfibcb = cbFibers.getSelectedIndex();
        int selectionepocb = cbEpoxy.getSelectedIndex();
        cbEpoxy.removeAllItems();
        cbFibers.removeAllItems();
        /* todo refactor
        Iterator<Epoxy> epoIt = Global.data.getEpoxys();
        while(epoIt.hasNext())
        {
            cbEpoxy.addItem(epoIt.next());
        }
        Iterator<Fiber> fibIt = Global.data.getFibers();
        while(fibIt.hasNext())
        {
            cbFibers.addItem(fibIt.next());
        }*/
        txtVf.setText(String.valueOf(vf));
        String test = txtVf.getText();
        double tempvf = (vf*1000);
        manualChange = false;
        slVf.setValue((int)tempvf);
        int testsl = slVf.getValue();

        btnEpoxyColor.setBackground(epoxyColor);
        btnFiberColor.setBackground(fiberColor);

        examp.setTheta(0);
        examp.setSet(new ColorSet(epoxyColor, fiberColor));
        examp.setSoort(soort);

        examp.repaint();

        this.validate();
        this.repaint();
        cbEpoxy.setSelectedIndex(selectionepocb);
        cbFibers.setSelectedIndex(selectionfibcb);
    }

    private void getEpoxySelection() {
        e = (Epoxy)cbEpoxy.getSelectedItem();
        epoxyColor = e.getColor();
        updateForm();
    }
}
